clear textprogressbar; clear; clc;
%% Create session, Connect devices
session = MeasurementSession.TestSession.CreateSession;
session.create_new_monitor;
pm = EquipmentControl.SystemlMachine.ParametricMagnetometer(session);
% input('probe volt = 0.4 V');
%%
phase_setting_data = pm.setDemodPhase('isPlot', true);
pause(1);

%% Scope
clear textprogressbar; 

pm.setZ1CoilOffset(0.05);

probe_waveform = pm.getProbeSignal('domain', 'time');
probe_spectrum = pm.getProbeSignal('domain', 'frequency');

%%
clear textprogressbar; 
std_spectrum=pm.getStdSpectrum();

%%
session.addParameter('Temperature', 110, 'degreeC');
session.addParameter('Pupm_Inc_Power', 165, 'mW');
session.addParameter('Probe_Inc_Power', 2.244, 'mW');
session.addParameter('Probe_Power_To_PD', 0.359, 'mW');
session.addMeasurementData('phase_settings', phase_setting_data);
session.addMeasurementData('probe_waveform', probe_waveform);
session.addMeasurementData('probe_spectrum', probe_spectrum);
session.addMeasurementData('spectrum_res', std_spectrum);

%% Finalize
session.close();
MeasurementSession.save(session);

%%
import EquipmentControl.AnalyseEquipment.ZI.ziData.*

scopeTimeData = ziScopeData(probe_waveform);
scopeFreqData = ziScopeData(probe_spectrum, 'domain', 'frequency');
spectrum_data = ziSpectrumData(std_spectrum, 'sample_name', 'sample_x_fft_abs_avg');

figure;

ax1 = subplot(2, 2, [1 3]);
plot(ax1, scopeTimeData.getArray().time * 1e3, scopeTimeData.getArray().value);
xlim(ax1, minmax(scopeTimeData.getArray().time) * 1e3);
xlabel(ax1, 'Time (ms)'); ylabel(ax1, 'Probe Signal (V)');
legend({'Probe Signal', 'Parametric Driving Signal'})

ax2 = subplot(2, 2, 2);
spectrum_analyser(scopeFreqData.getArray(), 'fmin', 33.5e3, 'fmax', 34.5e3, ...
                  'MinPeakHeight', 1e-3, 'MinPeakProminence', 1, 'figure_hdl', ax2);
xlabel(ax2, 'Frequency (Hz)'); ylabel(ax2, 'FFT Probe Signal (V\cdotHz^{-1/2})');

ax3 = subplot(2, 2, 4);
spectrum_analyser(spectrum_data.getArray('node_index', 4), 'fmin', 40, 'fmax', 90', ...
                  'MinPeakHeight', 1e-4, 'MinPeakProminence', 1, 'figure_hdl', ax3);
xlabel(ax3, 'Frequency (Hz)'); ylabel(ax3, 'DemodX Spectrum (V\cdotHz^{-1/2})');